package tongcheng.com.tongcheng.base.main.activity;

import android.content.Context;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.yanzhenjie.nohttp.RequestMethod;

import java.util.ArrayList;
import java.util.List;

import butterknife.Bind;
import tongcheng.com.tongcheng.ConStans;
import tongcheng.com.tongcheng.R;
import tongcheng.com.tongcheng.base.BaseActivity;
import tongcheng.com.tongcheng.bean.XiaoXiLieBiaoBean;
import tongcheng.com.tongcheng.network.CallServer;
import tongcheng.com.tongcheng.network.MyRequest;
import tongcheng.com.tongcheng.network.OnCallBack;
import tongcheng.com.tongcheng.utils.GlideApp;
import tongcheng.com.tongcheng.utils.GsonUtil;
import tongcheng.com.tongcheng.utils.Setting;

public class MessageActivity extends BaseActivity {

    @Bind(R.id.back)
    ImageView mBack;
    @Bind(R.id.title)
    TextView mTitle;
    @Bind(R.id.title_bar)
    RelativeLayout mBg;
    @Bind(R.id.lv_xiaoXi)
    ListView lv_xiaoXi;
    private XiaoXiAdapter adapter;
    private List<XiaoXiLieBiaoBean.DataBean> data;

    @Override
    public Integer initResid() {
        return R.layout.activity_setting;
    }

    @Override
    public void initCreate(Bundle savedInstanceState) {
        settitlebar(mBg);
        mBack.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                finish();
            }
        });

        mTitle.setText("消息");

        data = new ArrayList<>();
        adapter = new XiaoXiAdapter(this,data);
        lv_xiaoXi.setAdapter(adapter);

        getXiaoXiLieBiao();

    }

    private void getXiaoXiLieBiao() {
        MyRequest request = new MyRequest(ConStans.XiaoXiLieBiaoUrl, RequestMethod.GET);
        request.add("userid", Setting.getId());
        CallServer.getInstance().addGet(3, new OnCallBack() {
            @Override
            public void OnSuccess(int what, String json, Object ject) {
                Log.e("消息列表",json + "");
                data.clear();
                XiaoXiLieBiaoBean bean = GsonUtil.GsonToBean(json,XiaoXiLieBiaoBean.class);
                data = bean.getData();

                adapter.setData(data);
                adapter.notifyDataSetChanged();
            }
        }, request);
    }

    public class XiaoXiAdapter extends BaseAdapter{
        private Context mContext ;
        private List<XiaoXiLieBiaoBean.DataBean> data;

        public XiaoXiAdapter(Context mContext, List<XiaoXiLieBiaoBean.DataBean> data) {
            this.mContext = mContext;
            this.data = data;
        }

        @Override
        public int getCount() {
            return data.size();
        }

        @Override
        public Object getItem(int i) {
            return data.get(i);
        }

        @Override
        public long getItemId(int i) {
            return i;
        }

        @Override
        public View getView(int i, View view, ViewGroup viewGroup) {
            ViewHolder holder;
            if(view == null){
                view = LayoutInflater.from(mContext).inflate(R.layout.item_xiaoxi_liebiao,null);
                holder = new ViewHolder();
                holder.tv_time = view.findViewById(R.id.tv_time);
                holder.tv_title = view.findViewById(R.id.tv_title);
                holder.tv_date = view.findViewById(R.id.tv_date);
                holder.tv_content = view.findViewById(R.id.tv_content);
                holder.iv_content = view.findViewById(R.id.iv_content);
                view.setTag(holder);
            }else {
                holder = (ViewHolder) view.getTag();
            }
            XiaoXiLieBiaoBean.DataBean xiaoxi = data.get(i);
            holder.tv_time.setText(xiaoxi.getTitle_date());
            holder.tv_title.setText(xiaoxi.getTitle());
            holder.tv_date.setText(xiaoxi.getComment_date());
            holder.tv_content.setText(xiaoxi.getContent());
            String url = xiaoxi.getImg_url();
            if(!"".equals(url)){
                GlideApp.with(mContext).load(xiaoxi.getImg_url()).into(holder.iv_content);
            }else {
                holder.iv_content.setVisibility(View.GONE);
            }

            return view;
        }

        class ViewHolder{
            private TextView tv_time,tv_title,tv_date,tv_content;
            private ImageView iv_content;
        }

        public void setData(List<XiaoXiLieBiaoBean.DataBean> data) {
            this.data = data;
        }
    }

}
